package com.example.backend.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.backend.api.common.Result;
import com.example.backend.api.entity.Admin;
import com.example.backend.api.mapper.AdminMapper;
import com.example.backend.api.service.AdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xwyzsn
 * @since 2023-03-07
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    public Result login(String id, String password) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id);
        Admin admin = adminMapper.selectOne(queryWrapper);

        if (admin != null) {
            String password1 = admin.getPassword().trim();
            if (!password1.equals(password.trim())) {
                return Result.fail("用户名或密码错误");
            }
            else {
                return Result.success(admin);
            }
        }
        return Result.fail("用户名或密码错误");
}

    public Result selectAll() {
        return Result.success(adminMapper.selectList(null));
    }

    public Result delete(String id) {
        UpdateWrapper<Admin> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", id);
        int res = adminMapper.delete(updateWrapper);
        return Result.success(res);
    }

    public Result update(String id, String name, String tel) {
        UpdateWrapper<Admin> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", id);
        if (name != null) {
            updateWrapper.set("name", name);
        }
        if (tel != null) {
            updateWrapper.set("tel", tel);
        }
        int res = adminMapper.update(null, updateWrapper);
        return Result.success(res);
    }

    public Result add(String id, String name, String tel) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id);
        Admin admin1 = adminMapper.selectOne(queryWrapper);
        if (admin1 != null) {
            return Result.fail("用户已存在");
        }

        Admin admin = new Admin();
        admin.setUserId(id);
        admin.setName(name);
        admin.setTel(tel);
        admin.setCreatedBy("s");
        admin.setCreatedTime(new Date());
        admin.setPassword("123456");
        admin.setRole("admin");

        int res = adminMapper.insert(admin);
        return Result.success(res);
    }

    public Result changePassword(String id, String oldPassword, String newPassword) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id);
        Admin admin = adminMapper.selectOne(queryWrapper);
        if (admin == null) {
            return Result.fail("用户不存在");
        } else if (admin.getPassword().compareTo(oldPassword) != 1) {
            return Result.fail("旧密码错误");
        } else {
            UpdateWrapper<Admin> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("user_id", id);
            updateWrapper.set("password", newPassword);
            int res = adminMapper.update(null, updateWrapper);
            return Result.success(res);
        }

    }
}
